import { toJS } from 'mobx';
import { CHECKED } from "../../../../../constants/common";
import { formatSubTitle } from "../../../../../utils/format";

export default {
  required: {
    validate: ({ value }) => {

      if(value && value.length) {
        return value.some(item => item.key !== '' )
      }

      return false;
    }
  },
  limit: {
    validate: ({ value, config }) => {
      const { max, min } = config;
      const len = toJS(value).filter(c=>c.key !== "").length;
      let isValid = true;

      if (CHECKED === max.checked) {
        isValid = len <= max.value;
      }

      if (CHECKED === min.checked) {
        isValid = isValid && len >= min.value;
      }

      return isValid;
    },
    error: ({ config }) => formatSubTitle(config.subTitle)
  }
};